angular.module('charityApp').component('mediaList', {
    templateUrl: 'app/mediaList/media-list.template.html',
    controller: ['$charitySocietyService', '$state', function ($charitySocietyService, $state) {
        var pp = this;
        pp.medias = [];
        this.queryArticles = function () {
            $charitySocietyService.getArticles({pageNo: pp.currentPage, articleType: 8}, function (data) {
                pp.medias = data.data;
                if (!pp.medias || pp.medias.length < 10) {
                    pp.lastPage = true;
                } else {
                    pp.lastPage = false;
                }
            }, function (err) {
                console.error(err);
            });
        };
        this.goClick = function (id) {
            $state.go('mediaDetail', {id: id});
        };
        this.prePageClick = function () {
            if (pp.currentPage > 0) {
                pp.currentPage = pp.currentPage - 1;
                this.queryArticles();
            }
        };
        this.nextPageClick = function () {
            if (pp.medias.length == 10) {
                pp.currentPage = pp.currentPage + 1;
                this.queryArticles();
                pp.lastPage = false;
            } else {
                pp.lastPage = true;
            }
        };
        this.init = function () {
            console.log('init');
            pp.currentPage = 0;
            this.queryArticles();
        };
        this.init();
    }]
});